# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Print(self, pRoot):
        # write code here
        if not pRoot :
            return []
        nodes = [pRoot]
        res = [[pRoot.val]]
        direction = 0# 等于0从右向左，等于1从左往右

        while nodes:
            layer_res = []
            for node in nodes:
                if node.left:
                    layer_res.append(node.left)
                if node.right:
                    layer_res.append(node.right)
            if direction:
                res.append([item.val for item in layer_res])
            else:
                res.append([item.val for item in layer_res[::-1]])

            nodes = layer_res
            direction = -direction + 1
        return res[:-1]







# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Print(self, pRoot):
        # write code here
        if not pRoot:
            return []
        self.count = 1
        self.res = [[pRoot.val]]

        def helper(node_list):
            if not node_list:
                return None
            layer = []
            res = []
            for node in node_list:
                if node:
                    if node.left:
                        res.append(node.left.val)
                        layer.append(node.left)
                    if node.right:
                        res.append(node.right.val)
                        layer.append(node.right)
            if res:
                if self.count % 2:
                    self.res += res[::-1],
                else:
                    self.res += res,
            self.count += 1
            helper(layer)

        helper([pRoot])
        return self.res

